/**
 * 
 */
function change_dang_theo_doi_kh(url,khaid,dang_theo_doi){
	var dtd=1-dang_theo_doi;
	var lang_ktd=COREJS._getJsLang('KHONG_THEO_DOI');
	var lang_dtd=COREJS._getJsLang('DANG_THEO_DOI');
	if(url == ''){
		url =COREJS._getJsLang('URL')+'/';
	}
	if(dtd==1){
		document.getElementById("dang_theo_doi_"+khaid).innerHTML = "<img src='"+url+"images/icons/flag-16.png' align='absmiddle' width='16px' height='16px' title='"+lang_dtd+"'   onclick=\"change_dang_theo_doi_kh('"+url+"','"+khaid+"','1');\" style='cursor:pointer;' />";
	}
	else{
		document.getElementById("dang_theo_doi_"+khaid).innerHTML = "<img src='"+url+"images/icons/flag-16-d.png' align='absmiddle' width='16px' height='16px' title='"+lang_ktd+"'  onclick=\"change_dang_theo_doi_kh('"+url+"','"+khaid+"','0');\" style='cursor:pointer;' />";
	}
	var urlaction=url+"modules/customers/add/changeflag?Kha_Id="+khaid+'&Dang_Theo_Doi='+dang_theo_doi;
	$.getJSON(urlaction,null,function(data){
	});
}
function delete_khach_hang(xoa_nhieu,id_xoa,gui_thu,khong_td,co_theo_doi,gui_sms){
	if(xoa_nhieu == 0){
		conf = confirm(COREJS._getJsLang('_ALERT_XOA_KH'));
		if(conf){
			window.location=COREJS._getJsLang('URL')+"/modules/customers/edit/deleteKH?KhaId="+id_xoa;
		}
	}
	else{
		var checked = false;
		var str = "";
		var count_check = 0;
		var danhsach_kh_checked = document.getElementsByName('kh_checked[]');
		for(i=0; i<danhsach_kh_checked.length; i++){
			if(danhsach_kh_checked[i].checked){
				checked = true;
				var kid = danhsach_kh_checked[i].value;
				if(gui_thu == 1){
					var tmp = 'email_' + kid;
				}
				if(id_xoa == 1){
					var tmp = 'coxoa_' + kid;
				}
				if(gui_thu == 1 || id_xoa == 1){
					if(document.getElementById(tmp).value == 1 || document.getElementById(tmp).value != ''){
						str =str + kid + ",";
						count_check++;
					}
				}
				if(khong_td==1 || co_theo_doi == 1){
					if($('#cosua_'+kid).val() == 1){
						str =str + kid + ",";
						count_check++;
					}
				}
				if(gui_sms == 1){
					str =str + kid + ",";
				}
			}
		}
		if(gui_thu == 1){
			if(count_check == 0){
				alert(COREJS._getJsLang('_ALERT_KHACH_HANG_KHONG_EMAIL'));
				return false;
			}
			else{
				window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?idkh="+str;
					
			}
		}
		if(gui_sms == 1){
			window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?idkh="+str;
		}
		if(id_xoa == 1){
			if(count_check == 0){
				alert(COREJS._getJsLang('_KHONG_DUOC_XOA_KH'));
				return false;
			}
			else{
				conf = confirm(COREJS._getJsLang('_ALERT_XOA_NHIEU_KH'));
				if(conf){
					$('#del_strkh').val(str);
					document.frmcr.submit();
				}
			}
		}
		if(khong_td==1){
			if(count_check == 0){
				alert(COREJS._getJsLang('_NHUNG_KH_DA_CHON_KHONG_CO_QUYEN_SUA'));
				return false;
			}
			else{
				$('#khktd').val(str);
				document.frmcr.submit();
			}
			
		}
		if(co_theo_doi==1){
			if(count_check == 0){
				alert(COREJS._getJsLang('_NHUNG_KH_DA_CHON_KHONG_CO_QUYEN_SUA'));
				return false;
			}
			else{
				$('#khctd').val(str);
				document.frmcr.submit();
			}
			
		}
		
	}
}
LIST = {
	show_down_menu:function(e){
		toogleOpen(e);
	}
}
$(document).ready(function(){
	//$("#large").tablesorter();
	$('.m-n-d').click(function(){
		$(this).css('display','block');
	});
	$('.list .row01:odd').css('background-color','#FAFAFA');
	$('#checkall').click(function(){
		var countcheck = 0;
		if($(this).is(':checked')){
			$('.list input[type="checkbox"]').each(function(){
				$(this).attr('checked',true);
				countcheck++;
			});
			$('.l-bt-hd-s #count').text(countcheck);
			$('.l-bt-hd-s').css('display','block');
			$('.l-bt-hd-n-s').css('display','none');
		}
		else
		{
			$('tbody .list input[type="checkbox"]').each(function(){
				$(this).attr('checked',false);
			});
			$('.l-bt-hd-s #count').text(0);
			$('.l-bt-hd-s').css('display','none');
			$('.l-bt-hd-n-s').css('display','block');
		}
	});
	$('.list input[type = "checkbox"]').click(function(){
		var countcheck = Number($('.l-bt-hd-s #count').text());
		if($(this).is(':checked'))
		{
			countcheck++;
		}
		else
		{
			countcheck--;
		}
		if(countcheck===0)
		{
			$('.l-bt-hd-s').css('display','none');
			$('.l-bt-hd-n-s').css('display','block');
		}
		else
		{
			$('.l-bt-hd-s').css('display','block');
			$('.l-bt-hd-n-s').css('display','none');
		}
		$('.l-bt-hd-s #count').text(countcheck);
	});
	//onload
		$('#checkall').attr('checked',false);
		var countcheck = 0;
		if($('#checkall').is(':checked')){
			$('tbody .list input[type="checkbox"]').each(function(){
				$(this).attr('checked',true);
				countcheck++;
			});
			$('.l-bt-hd-s #count').text(countcheck);
			$('.l-bt-hd-s').css('display','block');
			$('.l-bt-hd-n-s').css('display','none');
		}
		else
		{
			$('tbody .list input[type="checkbox"]').each(function(){
				$(this).attr('checked',false);
			});
			$('.l-bt-hd-s #count').text(0);
			$('.l-bt-hd-s').css('display','none');
			$('.l-bt-hd-n-s').css('display','block');
		}
	
});
function show_first_page(frm, url, page){
	var objForm = document.getElementById(frm)
	var objPage = document.getElementById(page)
	objPage.selectedIndex = 0
	objForm.action = url
	objForm.submit()
}

function show_prev_page(frm, url, page){
	var objForm = document.getElementById(frm)
	var objPage = document.getElementById(page)
	objPage.selectedIndex = objPage.selectedIndex-1
	objForm.action = url
	objForm.submit()
}

function show_next_page(frm, url, page){
	var objForm = document.getElementById(frm)
	var objPage = document.getElementById(page)
	objPage.selectedIndex = objPage.selectedIndex+1
	objForm.action = url
	objForm.submit()
}

function show_last_page(frm, url, page){
	var objForm = document.getElementById(frm)
	var objPage = document.getElementById(page)
	objPage.selectedIndex = objPage.options.length - 1
	objForm.action = url
	objForm.submit()
}
$(function() {
    $( "#frm_searchkh" ).dialog({
        autoOpen: false,
        height:470,
        width:800,
        modal: true
    });
    $(".search_khachhang").click(function(){
		document.getElementById('titi_popup_search').style.display='none';
        $( "#frm_searchkh" ).dialog( "open" );
    })
});
function close_popup_searchkh(){
	  $( "#frm_searchkh" ).dialog( "close" );
}
function check_canhan(){
	
    canhan=document.getElementById('ip_khcn').value;
	document.getElementById('ip_khcn').value=1-canhan*1;
    if(canhan==1){
        document.getElementById('khcn').style.display='none';
    }
    else{
        document.getElementById('khcn').style.display='block';
    }
    
}

function check_tochuc(){
	var  tc = document.getElementById('ip_khtc').value;
	 document.getElementById('ip_khtc').value = 1-tc*1;
    if(tc==1){
        document.getElementById('khtc').style.display='none';
    }
    else{
        document.getElementById('khtc').style.display='block';
    }
   
}
function show_by_char(frm, url, char){
	var hiddenChar = document.getElementById('char')
	var objForm = document.getElementById(frm)
	
	objForm.action = url
	hiddenChar.value = char
	objForm.submit()
}
function close_popup_spqt(){
    $( "#dialog1" ).dialog( "close" );
}
$(function() {
    $( "#dialog1" ).dialog({
        autoOpen: false,
        height: 600,
        width:500,
        modal: false
    });
    $(".openp").click(function(){
		document.getElementById('titi_popup_spqt').style.display='none';
        $( "#dialog1" ).dialog( "open" );
    })
});
function close_popup_spdm(){
    $( "#sanphamdm" ).dialog( "close" );
}
$(function() {
    $( "#sanphamdm" ).dialog({
        autoOpen: false,
        height: 600,
        width:500,
        modal: true
    });
    $(".openpp_spdm").click(function(){
		document.getElementById('titi_popup_spdm').style.display='none';
        $( "#sanphamdm" ).dialog( "open" );
    })
});

$(function() {
	$('.openp_dialog3').click(function(){
		document.getElementById('titi_popup_nqlchbh').style.display='none';
		$("#dialog3").dialog('open');
	});
	$('.openp_dialog2').click(function(){
		document.getElementById('titi_popup_nql').style.display='none';
		$("#dialog2").dialog('open');
	});
   
	$("#dialog3").dialog({
    	autoOpen: false,
    	height: 500,
    	width: 500,
    	modal: false
    });
	$("#dialog2").dialog({
    	autoOpen: false,
    	height: 500,
    	width: 500,
    	modal: false
    });
});
function close_popup_nqlk(){
    $("#dialog2").dialog("close");
}
function toUnformattedNumber(id_row){
	document.getElementById(id_row).value=document.getElementById(id_row+'_').value;
}
CHBH = {
	chuyenQL:function(url)
	{
			if(($('#HDNguoi_Ql_C').val()=='') && ($('#ds_nql').val()==''))
			{
			
				return false;
			}
			else
			{
			$('#chuyen_ql').dialog('close');
			}
			
		//var danhsach_kh_checked = document.getElementsByName('kh_checked[]');
		var str = $("#array_cnql_khid").val();
		/*for(i=0; i<danhsach_kh_checked.length; i++){
			if(danhsach_kh_checked[i].checked){
				checked = true;
				var kid = danhsach_kh_checked[i].value;
				str =str + kid + ",";
			}
		}*/
		var cus_id = $("#HDNguoi_Ql_C").val();
		var dvi_id = $("#HDDV_Ql_C_ID").val();
		var nguoi_qlk = $("#dsid_nql").val();
		if(document.getElementById('chia_se').checked){
			var chia_se = 1;
		}
		else{
			var chia_se = 0;
		}
		var qstr = "type="+dvi_id+'-'+cus_id+'-'+str+'-'+nguoi_qlk+'-'+chia_se;
		window.location=COREJS._getJsLang('URL')+"/modules/customers/KhIndex?"+qstr;
	}
}
 function lefmenu_Tip(content, cat, time){
		Tip('<div style=\"text-align:left;\"><i style=\"color:#2A75C5\">'+cat+'</i><br />'+content+'<br /><span style="color:#aaaaaa;">'+COREJS._getJsLang('_VAO_LUC')+': '+time+'</span></div>', SHADOW, true, SHADOWCOLOR, '#ffbbcc', BORDERCOLOR, '#555555', BGCOLOR, '#ffffe1', WIDTH, 200);
}
function show_tree(id){
	var objSub=document.getElementById(id);
	var url = COREJS._getJsLang('URL');
	if(objSub){
		if(objSub.style.display=="block"){
			  $('.'+id).slideToggle();   
			
			var im = "<img src='"+url+"/images/icons/expan_icon.png' border='0' align='absmiddle' onclick=\"show_tree('"+id+"');\" />";
			$("#show_"+id).html(im);
		}
		else{
			  $('.'+id).slideToggle();   
			var im = "<img src='"+url+"/images/icons/close_icon.png' border='0' align='absmiddle' onclick=\"show_tree('"+id+"');\" />";
			$("#show_"+id).html(im);
		}
	}
}
function customer_tooltip_show(id){
    var tt = $('#'+id).html();
	Tip(tt, STICKY, true, CLICKCLOSE, true, PADDING, 1);
}
function check_loc_kh(id_vl){
	$('#chuacocv').val('');
	$('#day').val('');
	$('#month').val('');
	$('#txd').val('');
	$('#txm').val('');
	$('#dgd').val('');
	$('#kgd').val('');
	$('#dangcoch').val('');
	$('#topkhtn').val('');
	$('#topkhtx').val('');
	if(id_vl!='tungayloc'){
		$('#tungayloc').val('');
		$('#denngayloc').val('');
	}
	$('#dk_loc').val(id_vl);
	if(id_vl !='' && id_vl!='tungayloc'){
		$('#'+id_vl).val(1);
	}
	else{
		if($.trim($('#tungaylockh').text())!=COREJS._getJsLang('_TU_NGAY')){
			$('#tungayloc').val($.trim($('#tungaylockh').text()));
		}
		if($.trim($('#denngaylockh').text())!=COREJS._getJsLang('_DEN_NGAY')){
			$('#denngayloc').val($.trim($('#denngaylockh').text()));
		}
	}
	document.frmcr.submit();
}
function edit_customers(checksua,check_cnql){
	var checked = false;
	var str = "";
	var strcn = "";
	var strtc = "";
	var strdchi = "";
	var count_check = 0;
	var canhan = 0;
	var tochuc = 0;
	var danhsach_kh_checked = document.getElementsByName('kh_checked[]');
	for(i=0; i<danhsach_kh_checked.length; i++){
		if(danhsach_kh_checked[i].checked){
			checked = true;
			var kid = danhsach_kh_checked[i].value;
			var tmp = 'cosua_' + kid;
			if($('#'+tmp).val() == 1){
				str =str + kid + ",";
				count_check++;
				if(checksua == 1){
					strdchi = strdchi + $('#dchid_'+kid).val()+ ",";
					
					if($('#ca_nhan_'+kid).val() == 1){
						canhan++;
						strcn = strcn + $('#khcnid_'+kid).val()+ ",";
					}
					else{
						tochuc++;
						strtc = strtc + $('#khtcid_'+kid).val()+ ",";
					}
				}
			}
		}
	}
	if(count_check == 0){
		alert(COREJS._getJsLang('_NHUNG_KH_DA_CHON_KHONG_CO_QUYEN_SUA'));
		return false;
	}
	else{
		if(checksua == 1){
			$('#array_khid').val(str);
			$('#array_khcnid').val(strcn);
			$('#array_khtcid').val(strtc);
			$('#array_dchid').val(strdchi);
			
			if(tochuc != 0 && canhan !=0){
				document.getElementById('khtc_left').style.display='none';
				document.getElementById('khtc_right').style.display='none';
				document.getElementById('khcn_left').style.display='none';
				document.getElementById('khcn_right').style.display='none';
			}
			else if(tochuc != 0){
				document.getElementById('khtc_left').style.display='block';
				document.getElementById('khtc_right').style.display='block';
				document.getElementById('khcn_left').style.display='none';
				document.getElementById('khcn_right').style.display='none';
			}
			else{
				document.getElementById('khtc_left').style.display='none';
				document.getElementById('khtc_right').style.display='none';
				document.getElementById('khcn_left').style.display='block';
				document.getElementById('khcn_right').style.display='block';
			}
			$(function() {
				$( "#openp_edit_customes" ).dialog({
						autoOpen: false,
						height: 320,
						width:800,
						modal: true
					});
				 $( "#openp_edit_customes" ).dialog( "open" );
				
			});
		}
		if(check_cnql == 1){
			$('#array_cnql_khid').val(str);
			 $( "#chuyen_ql" ).dialog({
				bgiframe: true,
				autoOpen: false,
				height: 200,
				width:500,
				modal: true
			});
			$( "#chuyen_ql" ).dialog( "open" );
			document.getElementById('titi_popup_cql').style.display='none';
		}
	}
}
function  close_popup_editct(){
    $("#openp_edit_customes").dialog("close");
}
function edited_customers(){
	var getFormValues = $('#frmEditCustomers').serialize();
	window.location=COREJS._getJsLang('URL')+"/modules/customers/editedCTS?"+getFormValues;
}
function showTipWork(nd){
		tipWidth =  170;
		Tip('<div style="float:left;text-align:left; width:250px;">'+nd+'</div>', SHADOW, true, SHADOWCOLOR, '#ffbbcc', BORDERCOLOR, '#555555', BGCOLOR, '#ffffe1', WIDTH, tipWidth);
}
function gui_em_sms(gui_thu,gui_sms){
	var checked = false;
	var str = "";
	var strkh_email = "";
	var strkh_dd = "";
	var strndd = "";
	var strdmll = '';
	var count_check = 0;
	var canhan = 0;
	var tochuc = 0;
	var so_khkndd = 0;
	var str_khkdd = '';
	var str_khkdmll = '';
	var so_khkdmll = 0;
	var danhsach_kh_checked = document.getElementsByName('kh_checked[]');
	for(i=0; i<danhsach_kh_checked.length; i++){
		if(danhsach_kh_checked[i].checked){
			checked = true;
			var kid = danhsach_kh_checked[i].value;
			str =str + kid + ",";
			//check email
			var tmp = 'email_' + kid;
			if($('#email_'+kid).val()!='' && gui_thu == 1){
				count_check++;
				strkh_email = strkh_email + kid + ",";
			}
			if($('#dtdd_'+kid).val()!='' && gui_sms == 1){
				count_check++;
				strkh_dd = strkh_dd + kid + ",";
			}
			
			if($('#ca_nhan_'+kid).val() == 1){
				canhan++;
			}
			else{
				tochuc++;
				if($('#nguoidd_'+kid).val()!=''){
					strndd = strndd + $('#nguoidd_'+kid).val()+ ",";
				}
				else{
					//danh sach ten khach hang ko co nguoi dai dien
					so_khkndd++;
					str_khkdd = str_khkdd + kid+ ",";
				}
				if($('#list_dmll_id_'+kid).val()!=''){
					strdmll = strdmll + $('#list_dmll_id_'+kid).val()+ ",";
				}
				else{
					so_khkdmll++;
					str_khkdmll = str_khkdmll + kid+ ",";
				}
			}
		}
	}
	if(tochuc == 0){
		if(gui_thu == 1){
			if(count_check == 0){
				alert(COREJS._getJsLang('_ALERT_KHACH_HANG_KHONG_EMAIL'));
				return false;
			}
			else{
				window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?idkh="+strkh_email;
					
			}
		}
		if(gui_sms == 1){
			window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?idkh="+strkh_dd;
		}
	}
	else{
		$('#array_ndd').val(strndd);
		$('#array_dmll').val(strdmll);
		$('#array_khemail').val(strkh_email);
		$('#array_khdtdd').val(strkh_dd);
		$('#check_gui_sms').val(gui_thu);
		$('#so_khkndd').val(so_khkndd);
		$('#str_khkdd').val(str_khkdd);
		$('#so_khkdmll').val(so_khkdmll);
		$('#str_khkdmll').val(str_khkdmll);
		$(function() {
			$( "#openp_sms_mail" ).dialog({
					autoOpen: false,
					height: 160,
					width:200,
					modal: true
				});
			 $( "#openp_sms_mail" ).dialog( "open" );
			
		});
		if(gui_thu==1){
			document.getElementById('check_gui_mail_khtc').style.display='block';
			$('#ui-dialog-title-openp_sms_mail').text(COREJS._getJsLang('GUI_MAIL'));
			$('#check_button_ds').val(COREJS._getJsLang('GUI_MAIL'));
		}
		else{
			document.getElementById('check_gui_mail_khtc').style.display='none';
			$('#ui-dialog-title-openp_sms_mail').text(COREJS._getJsLang('GUI_SMS'));
			$('#check_button_ds').val(COREJS._getJsLang('GUI_SMS'));
		}
	}
	
}
function check_send_mail_sms(){
	//kiem tra check chon gui kieu gi
	var type_check = $('#type_check').val();
	var check_gui_sms = $('#check_gui_sms').val();
	if(type_check == 1){
		var array_ndd = $('#array_ndd').val();
		if(array_ndd==""){
			alert(COREJS._getJsLang('_NHUNG_KHACH_HANG_DA_CHON_KC_NDD'));
			return false;
		}
		else{
			if($('#so_khkndd').val()!=0){
				str = COREJS._getJsLang('_CO');
				str += "<a  href='"+COREJS._getJsLang('URL')+"/modules/customers/KhIndex?str_khaid="+$('#str_khkdd').val()+"'><span  class='redtext'><b>";
				str += $('#so_khkndd').val()+'</b></span></a> '+COREJS._getJsLang('_KHACH_HANG_KHONG_CO_NDD_BAN_CO_MUON_TT');
				COREJS.v_fShowYesNoDialog(str, 
					function(){
						if(check_gui_sms == 1){
							$(function(){
								if ($('#array_dmll').val()){
									var en_dmll = gnecpt($('#array_dmll').val() + "");
									$('#array_dmll').val(en_dmll);
								}
								if ($('#array_khdtdd').val()){
									var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
									$('#array_khdtdd').val(en_khdtdd);
								}
								if ($('#array_khemail').val()){
									var en_khemail = gnecpt($('#array_khemail').val() + "");
									$('#array_khemail').val(en_khemail);
								}
								if ($('#array_ndd').val()){
									var en_ndd = gnecpt($('#array_ndd').val() + "");
									$('#array_ndd').val(en_ndd);
								}
								
								$('<input>').attr({
									type: 'hidden',
									id: 'dmll',
									name: 'dmll',
									value: $('#array_ndd').val()
								}).appendTo('#frmSendmail');
								$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
								$('#frmSendmail').attr("method", "post");
								$('#frmSendmail').submit();
							});
						}
						else{
							window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?iddm="+array_ndd;
						}
					},
					function(){
						$('#check_button').focus();
					}
				);
			}
			else{
				if(check_gui_sms == 1){
					$(function(){
						if ($('#array_dmll').val()){
							var en_dmll = gnecpt($('#array_dmll').val() + "");
							$('#array_dmll').val(en_dmll);
						}
						if ($('#array_khdtdd').val()){
							var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
							$('#array_khdtdd').val(en_khdtdd);
						}
						if ($('#array_khemail').val()){
							var en_khemail = gnecpt($('#array_khemail').val() + "");
							$('#array_khemail').val(en_khemail);
						}
						if ($('#array_ndd').val()){
							var en_ndd = gnecpt($('#array_ndd').val() + "");
							$('#array_ndd').val(en_ndd);
						}
						
						$('<input>').attr({
							type: 'hidden',
							id: 'dmll',
							name: 'dmll',
							value: $('#array_ndd').val()
						}).appendTo('#frmSendmail');
						$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
						$('#frmSendmail').attr("method", "post");
						$('#frmSendmail').submit();
					});						
				}
				else{
					window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?iddm="+array_ndd;
				}
			}
		}
	}
	if(type_check == 2){
		var array_dmll = $('#array_dmll').val();
		if(array_dmll==""){
			alert(COREJS._getJsLang('_NHUNG_KHACH_HANG_DA_CHON_KC_DAUMOILL'));
			return false;
		}
		else{
			if($('#so_khkdmll').val()!=0){
				str = COREJS._getJsLang('_CO');
				str += "<a  href='"+COREJS._getJsLang('URL')+"/modules/customers/KhIndex?str_khaid="+$('#str_khkdmll').val()+"'><span  class='redtext'><b>";
				str += $('#so_khkdmll').val()+'</b></span></a> '+COREJS._getJsLang('_KHACH_HANG_KHONG_CO_DMLL_BAN_CO_MUON_TT');
				COREJS.v_fShowYesNoDialog(str, 
					function(){
						if(check_gui_sms == 1){
							$(function(){
								if ($('#array_dmll').val()){
									var en_dmll = gnecpt($('#array_dmll').val() + "");
									$('#array_dmll').val(en_dmll);
								}
								if ($('#array_khdtdd').val()){
									var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
									$('#array_khdtdd').val(en_khdtdd);
								}
								if ($('#array_khemail').val()){
									var en_khemail = gnecpt($('#array_khemail').val() + "");
									$('#array_khemail').val(en_khemail);
								}
								if ($('#array_ndd').val()){
									var en_ndd = gnecpt($('#array_ndd').val() + "");
									$('#array_ndd').val(en_ndd);
								}
								
								$('<input>').attr({
									type: 'hidden',
									id: 'dmll',
									name: 'dmll',
									value: $('#array_dmll').val()
								}).appendTo('#frmSendmail');
								$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
								$('#frmSendmail').attr("method", "post");
								$('#frmSendmail').submit();
							});
							//window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?iddm="+array_dmll;
						}
						else{
							window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?iddm="+array_dmll;
						}
					},
					function(){
						$('#check_button').focus();
					}
				);
			}
			else{
				if(check_gui_sms == 1){
					$(function(){
						if ($('#array_dmll').val()){
							var en_dmll = gnecpt($('#array_dmll').val() + "");
							$('#array_dmll').val(en_dmll);
						}
						if ($('#array_khdtdd').val()){
							var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
							$('#array_khdtdd').val(en_khdtdd);
						}
						if ($('#array_khemail').val()){
							var en_khemail = gnecpt($('#array_khemail').val() + "");
							$('#array_khemail').val(en_khemail);
						}
						if ($('#array_ndd').val()){
							var en_ndd = gnecpt($('#array_ndd').val() + "");
							$('#array_ndd').val(en_ndd);
						}
						
						$('<input>').attr({
							type: 'hidden',
							id: 'dmll',
							name: 'dmll',
							value: $('#array_dmll').val()
						}).appendTo('#frmSendmail');
						$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
						$('#frmSendmail').attr("method", "post");
						$('#frmSendmail').submit();
					});
					// window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?iddm="+array_dmll;
				}
				else{
					window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?iddm="+array_dmll;
				}
			}
			
			
		}
	}
	if(type_check == 3){
		if(check_gui_sms == 1){
			var array_khemail = $('#array_khemail').val();
			if(array_khemail==""){
				alert(COREJS._getJsLang('_ALERT_KHACH_HANG_KHONG_EMAIL'));
				return false;
			}
			else{
				$(function(){
					if ($('#array_dmll').val()){
						var en_dmll = gnecpt($('#array_dmll').val() + "");
						$('#array_dmll').val(en_dmll);
					}
					if ($('#array_khdtdd').val()){
						var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
						$('#array_khdtdd').val(en_khdtdd);
					}
					if ($('#array_khemail').val()){
						var en_khemail = gnecpt($('#array_khemail').val() + "");
						$('#array_khemail').val(en_khemail);
					}
					if ($('#array_ndd').val()){
						var en_ndd = gnecpt($('#array_ndd').val() + "");
						$('#array_ndd').val(en_ndd);
					}
					
					$('<input>').attr({
						type: 'hidden',
						id: 'idkh',
						name: 'idkh',
						value: $('#array_khemail').val()
					}).appendTo('#frmSendmail');
					$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
					$('#frmSendmail').attr("method", "post");
					$('#frmSendmail').submit();
				});
				//window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?idkh="+array_khemail;
			}
		}
		else{
			var array_khdtdd = $('#array_khdtdd').val();
			if(array_khdtdd==""){
				alert(COREJS._getJsLang('_ALERT_KHACH_HANG_KHONG_DTDD'));
				return false;
			}
			else{
				window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?idkh="+array_khdtdd;
			}
		}
	}
	if(type_check == 4){
		var array_dmll = $('#array_dmll').val();
		var array_ndd = $('#array_ndd').val();
		var array_khemail = $('#array_khemail').val();
		var array_khdtdd = $('#array_khdtdd').val();
		if(check_gui_sms == 1){
			$(function(){
				if ($('#array_dmll').val()){
					var en_dmll = gnecpt($('#array_dmll').val() + "");
					$('#array_dmll').val(en_dmll);
				}
				if ($('#array_khdtdd').val()){
					var en_khdtdd = gnecpt($('#array_khdtdd').val() + "");
					$('#array_khdtdd').val(en_khdtdd);
				}
				if ($('#array_khemail').val()){
					var en_khemail = gnecpt($('#array_khemail').val() + "");
					$('#array_khemail').val(en_khemail);
				}
				if ($('#array_ndd').val()){
					var en_ndd = gnecpt($('#array_ndd').val() + "");
					$('#array_ndd').val(en_ndd);
				}
				
				$('<input>').attr({
					type: 'hidden',
					id: 'idkh',
					name: 'idkh',
					value: $('#array_khemail').val()
				}).appendTo('#frmSendmail');
				$('<input>').attr({
					type: 'hidden',
					id: 'dmll',
					name: 'dmll',
					value: $('#array_ndd').val() + $('#array_dmll').val()
				}).appendTo('#frmSendmail');
				$('#frmSendmail').attr("action", COREJS._getJsLang('URL') + "/modules/mail/compose");
				$('#frmSendmail').attr("method", "post");
				$('#frmSendmail').submit();
			});
			//window.location=COREJS._getJsLang('URL')+"/modules/mail/compose?iddm="+array_ndd+array_dmll+'&idkh='+array_khemail;
		}
		else{
			window.location=COREJS._getJsLang('URL')+"/modules/sms/new/index?iddm="+array_ndd+array_dmll+'&idkh='+array_khdtdd;
		}
	}
}
function them_vao_cd(){
	var checked = false;
	var str = "";
	var danhsach_kh_checked = document.getElementsByName('kh_checked[]');
	for(i=0; i<danhsach_kh_checked.length; i++){
		if(danhsach_kh_checked[i].checked){
			checked = true;
			var kid = danhsach_kh_checked[i].value;
			str =str + kid + ",";
		}
	}
	$('#strkh_cd').val(str);
	$(function() {
		$( "#openp_them_cdmk" ).dialog({
				autoOpen: false,
				height: 150,
				width:400,
				modal: true
			});
		 $( "#openp_them_cdmk" ).dialog( "open" );
		
	});
}

function check_them_khch(){
	if($('#idmkt').val()==""){
		alert(COREJS._getJsLang('_BAN_CHUA_CHON_CHIEN_DICH'));
		$('#idmkt').focus()
		return false;
	}
	else{
		idmk = $('#idmkt').val();
		strkh_cd = $('#strkh_cd').val();
		Tth_Cd = $('#Tth_Cd').val();
		var urlaction = COREJS._getJsLang('URL')+"/modules/customers/KhIndex/themcdmk?idmk="+idmk+'&strkh_cd='+strkh_cd+'&Tth_Cd='+Tth_Cd;
			$.getJSON(urlaction,null,function(data){
				if(data == 1){
					$('#openp_them_cdmk').dialog('close');
					window.location=COREJS._getJsLang('URL')+"/modules/marketing/index/view?idmk="+idmk+'&tab=2';
				}
			});
	}
}
function show_cities(id){
	var select = document.getElementById("dm_tinh_thanh");
	var urlaction=COREJS._getJsLang('URL')+"/modules/customers/add/getTinhThanh?vmi_id="+id+'&thutu=0';
	$.getJSON(urlaction,null,function(data){
			select.innerHTML = data;
		});
}
function show_quanhuyen(tth_id,qhu,thutu){
	var select = document.getElementById("dm_quan_huyen");
	var urlaction=COREJS._getJsLang('URL')+"/modules/customers/add/getQuanHuyen?tth_id="+tth_id+'&qh_id='+qhu+'&thutu='+thutu;
	$.getJSON(urlaction,null,function(data){
		select.innerHTML = data;
		});
}
function send_em_sms(em,sms){
	if(check_select_list() == 1){
		if(em==1){
			//document.getElementById('check_gui_mail_khtc').style.display='block';
			$('#ui-dialog-title-openp_sms_mail_fs').text(COREJS._getJsLang('GUI_MAIL'));
			$('#check_button').val(COREJS._getJsLang('GUI_MAIL'));
			$('#check_send').val(1);
		}
		else{
			//document.getElementById('check_gui_mail_khtc').style.display='none';
			$('#ui-dialog-title-openp_sms_mail_fs').text(COREJS._getJsLang('GUI_SMS'));
			$('#check_button').val(COREJS._getJsLang('GUI_SMS'));
			$('#check_send').val(2);
		}
		$(function() {
			$( "#openp_sms_mail_fs" ).dialog({
					autoOpen: false,
					height: 160,
					width:200,
					modal: false
				});
			 $( "#openp_sms_mail_fs" ).dialog( "open" );
			
		});
	}
}
function rd_send_emsms(){
	var type_check = $('#type_check_send').val();
	$('#type_send').val(type_check);
	document.frmSearch.submit();
}
function them_vao_cds(){
	if(check_select_list() == 1){
		$(function() {
			$( "#openp_them_cdmkfs" ).dialog({
					autoOpen: false,
					height: 160,
					width:400,
					modal: false
				});
			 $( "#openp_them_cdmkfs" ).dialog( "open" );
			
		});
	}
}
function check_them_khcds(){
	
	if($('#idmkt_s').val()==""){
		alert(COREJS._getJsLang('_BAN_CHUA_CHON_CHIEN_DICH'));
		$('#tenmkt_s').focus()
		return false;
	}
	else{
		//$('#idmkt_spp').val('');
		$('#trang_thai_tcd').val($('#Trang_Thai_Tcd').val());
		$('#idcd_them').val($('#idmkt_s').val());
		document.frmSearch.submit();
	}
}
function check_dklock(){
	dklk=document.getElementById('ip_dklk').value;
	document.getElementById('ip_dklk').value=1-dklk*1;
    if(dklk==1){
        document.getElementById('dklk').style.display='none';
    }
    else{
        document.getElementById('dklk').style.display='block';
    }
}
function close_ttch(){
	toogleClose('trang_thai_ch');
}
function openpp_ghmk(){
	$(function() {
		$( "#open_ghmk" ).dialog({
				autoOpen: false,
				height: 200,
				width:280,
				modal: false
			});
		 $( "#open_ghmk" ).dialog( "open" );
		
	});
}
function checked_type(vl){
	var $radios = $('input:radio[name=loc_dls]');
	$radios.filter('[value="'+vl+'"]').attr('checked', true);
	document.frmSearch.submit();
}
function check_submit_sc(){
	if(check_select_list() == 1){
		document.frmSearch.submit();
	}
}
function check_select_list(){
	if($('#La_Khach_Hang').attr('checked') || $('#khach_hang_tn').attr('checked') || $('#khach_hang_ktd').attr('checked')){
		return 1;
	}
	else{
		alert(COREJS._getJsLang('_CHUA_CHON_DOI_TUONG_TIM_KIEM'));
		$('#La_Khach_Hang').focus();
		return false;
	}
}
function gnecpt(str) {
	var TextSize = str.length;
	var rndStr, tmpStr;
	var tmpEncode = new Array();
	var encodedString = "";
	for (var i = 0; i < TextSize; i++) {
		rndStr = Math.floor((Math.random() * 256) + 100);
		tmpStr = str.charCodeAt(i) ^ rndStr;
		tmpEncode[i] = String.fromCharCode(tmpStr, 2999, rndStr);
	}
	for (var i = 0; i < TextSize - 1; i++) {
		encodedString += tmpEncode[i] + String.fromCharCode(3000);
	}
	encodedString += tmpEncode[TextSize - 1];
	return encodedString;
}